package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoq.constant.Configration;
import com.guoq.mapper.AddressMapper;
import com.guoq.pojo.Address;
import com.guoq.pojo.AddressExample;
import com.guoq.pojo.AddressExample.Criteria;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.Result;
import com.guoq.service.IAddressService;
import com.guoq.utils.Strkit;

@Service("addressService")
public class AddressServiceImpl implements IAddressService {

	@Resource
	private AddressMapper addressMapper;

	@Override
	public Address getById(int id) {
		
		// TODO Auto-generated method stub
		AddressExample example = new AddressExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo((byte) Configration.NORMAL);		//状态正常的地址
		criteria.andIdEqualTo(id);
		List<Address> list = this.addressMapper.selectByExample(example);
		
		return list.get(0);
	}

	@Override
	public EUDataGridResult getList(String userId) {
		
		// TODO Auto-generated method stub
		AddressExample example = new AddressExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo((byte) Configration.NORMAL);		//状态正常的地址
		criteria.andUserIdEqualTo(userId);							//当前用户下的地址
		example.setOrderByClause("id desc");						//按id的倒叙排
		List<Address> list = addressMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		return result;
	}
	
	@Override
	public EUDataGridResult getPageList(Address bean, Integer page, Integer rows) {
		
		// TODO Auto-generated method stub
		AddressExample example = new AddressExample();
		Criteria criteria = example.createCriteria();
		
		String name = bean.getName();
		if(Strkit.notBlank(name)){
			criteria.andNameLike(name);
		}
		
		example.setOrderByClause("createtime desc");
		
		//分页处理
		PageHelper.startPage(page, rows);
		List<Address> list = addressMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Address> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public List<Address> selectByExample(AddressExample example) {
		// TODO Auto-generated method stub
		List<Address> list = addressMapper.selectByExample(example);
		return list;
	}

	@Override
	public Address selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		Address record = addressMapper.selectByPrimaryKey(id);
		return record;
	}

	@Override
	public int countByExample(AddressExample example, String userId) {
		// TODO Auto-generated method stub
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo((byte) Configration.NORMAL);		//状态正常的地址
		criteria.andUserIdEqualTo(userId);							//当前用户下的地址
		example.setOrderByClause("id desc");						//按id的倒叙排
		criteria.andUserIdEqualTo(userId);
		int count = addressMapper.countByExample(example);
		return count;
	}

	@Override
	public Result create(Address bean) throws Exception {

		Result result = this.insert(bean);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 
	 *<P>开发者:郭强
	 *<P>方法名: insert
	 *<P>描述:新增 
	 *@param bean
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	@Override
	public Result insert(Address bean){
		
		bean.setCreatetime(new Date());
		bean.setUpdatetime(new Date());
		addressMapper.insertSelective(bean);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(Address record) throws Exception {

		Result result = null;
		
		record.setStatus((byte) Configration.DELETE);	//删除
		record.setUpdatetime(new Date());
		int num = addressMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result update(Address record) throws Exception {

		Result result = null;
		int num = addressMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}
}
